package object3D;

import context.*;

import geometry3D.Point3D;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.GeneralPath;

import javax.media.j3d.Appearance;
import javax.media.j3d.Group;



// ***************************************************************************
// *** CLASS: InngangsTrapp
// ***************************************************************************
public class MassivTrapp extends Block implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  public static final Color murFarge=Color.lightGray;

  public static final double oppTrinn=0.18;
  public static final double innTrinn=0.25;

  private static final int maxTrinn=15;
  private Block trinn[]=new Block[maxTrinn];
  private Block trinnOpp[]=new Block[maxTrinn];

  private int kind;
  
  public MassivTrapp(String ident,Point3D p0,double width,double height,double depth,int kind)
  { super(ident,p0,width,height,depth);
    this.kind=kind;
    int n=(int)(width/innTrinn);
    switch(kind)
    { case NORTH: for(int i=0;i<n;i++)
      { trinn[i]=new Block("Trinn",p0.addY(height-oppTrinn*i-0.05).addZ(innTrinn*i-0.05),depth,0.05,innTrinn+0.05);
        trinnOpp[i]=new Block("OppTrinn",p0.addY(height-oppTrinn*(i+1)-0.05+0.05).addZ(innTrinn*(i+1)-0.05),depth,oppTrinn-0.05,0.001);
      } break;
      case SOUTH: for(int i=0;i<n;i++)
      { trinn[i]=new Block("Trinn",p0.addY(height-oppTrinn*(n-1-i)-0.05).addZ(innTrinn*i),depth,0.05,innTrinn+0.05);
        trinnOpp[i]=new Block("OppTrinn",p0.addY(height-oppTrinn*(n-i)-0.05+0.05).addZ(innTrinn*(i)+0.05),depth,oppTrinn-0.05,0.001);
      } break;
      case WEST:  for(int i=0;i<n;i++)
      { trinn[i]=new Block("Trinn",p0.addY(height-oppTrinn*i-0.05).addX(innTrinn*i),innTrinn+0.05,0.05,depth);
        trinnOpp[i]=new Block("OppTrinn",p0.addY(height-oppTrinn*(i+1)).addX(innTrinn*(i+1)),0.001,oppTrinn-0.05,depth);
      } break;
      case EAST:  for(int i=0;i<n;i++)
      { trinn[i]=new Block("Trinn",p0.addY(height-oppTrinn*(n-1-i)-0.05).addX(innTrinn*i),innTrinn+0.05,0.05,depth);
        trinnOpp[i]=new Block("OppTrinn",p0.addY(height-oppTrinn*(n-i)).addX(innTrinn*(i)+0.05),0.001,oppTrinn-0.05,depth);
      } break;
    }
  }
    
  // ***************************************************************************
  // *** METHOD: getModel3D
  // ***************************************************************************
  public /*abstract*/ Group getModel3D(Appearance appearance)
  { Group tg=new Group();
    for(int i=0;i<maxTrinn;i++)
    {
      if(trinn[i]!=null) tg.addChild(trinn[i].getModel3D(appearance));
      if(trinnOpp[i]!=null) tg.addChild(trinnOpp[i].getModel3D(appearance));
      }
    return(tg);
  }  


  public void drawProjection(int direction,Graphics2D g,Point2D.Double origo2D,double scale,Color c)
  {

    Rectangle2D.Double r=projection(direction,origo2D,scale);
    switch(direction)
    {
      case NORTH: case SOUTH: case EAST: case WEST:
        tegnTrapp(direction,g,r,origo2D,scale,c); break;
//      case ABOVE: drawProjectionAbove(g,r,scale,c); break;
//      case BELOW: drawProjectionBelow(g,r,scale,c); break;
//      case VCUT: drawVerticalCut(g,r,scale,c); break;
      case HCUT:
        tegnTrappPlan(g,r,origo2D,scale,c);
//        tegnTrapp(direction,g,r,origo2D,scale,c);
        break;
      default: super.drawProjection(direction,g,origo2D,scale,c);
    }  
  }

  private void tegnTrapp(int direction,Graphics2D g,Rectangle2D.Double r,Point2D.Double origo2D,double scale,Color c)
  { for(int i=4;i>=0;i--)
    {
    if(trinnOpp[i]!=null) trinnOpp[i].drawProjection(direction,g,origo2D,scale,murFarge);
      if(trinn[i]!=null) trinn[i].drawProjection(direction,g,origo2D,scale,murFarge);
    }
  }

  private void tegnTrappPlan(Graphics2D g,Rectangle2D.Double r,Point2D.Double origo2D,double scale,Color c)
  {
    tegnTrapp(HCUT,g,r,origo2D,scale,c);
    // Tegn PIL !!!!
    float x0=(float)r.x;
    float y0=(float)(r.y+r.height/2);
    float xT=(float)(r.x+(innTrinn*3)*scale);
    float yT=(float)(r.y+r.height/2);
    float dx=(float)(0.20*scale);
    float dy=(float)(0.15*scale);
    GeneralPath path=new GeneralPath();
    switch(kind)
    {
      case NORTH:
        x0=(float)(r.x+r.height/2);
        y0=(float)(r.y+r.width);
        xT=(float)(r.x+r.height/2);
        yT=(float)(r.y);
        dx=(float)(0.15*scale);
        dy=(float)(0.20*scale);
        path.moveTo(x0,y0);
        path.lineTo(xT,yT);
        path.lineTo(xT+dx,yT+dy);
        path.moveTo(xT-dx,yT+dy);
        path.lineTo(xT,yT);
      break;
      case SOUTH:
        x0=(float)(r.x+r.height/2);
        y0=(float)(r.y);
        xT=(float)(r.x+r.height/2);
        yT=(float)(r.y+r.width);
        dx=(float)(0.15*scale);
        dy=(float)(0.20*scale);
        path.moveTo(x0,y0);
        path.lineTo(xT,yT);
        path.lineTo(xT+dx,yT-dy);
        path.moveTo(xT-dx,yT-dy);
        path.lineTo(xT,yT);
        break;
      case EAST:
        x0=(float)r.x;
        y0=(float)(r.y+r.height/2);
        xT=(float)(r.x+r.width);
        yT=(float)(r.y+r.height/2);
        dx=(float)(0.20*scale);
        dy=(float)(0.15*scale);
        path.moveTo(x0,y0);
        path.lineTo(xT,yT);
        path.lineTo(xT-dx,yT+dy);
        path.moveTo(xT-dx,yT-dy);
        path.lineTo(xT,yT);
        break;
      case WEST:
        x0=(float)(r.x+r.width);
        y0=(float)(r.y+r.height/2);
        xT=(float)r.x;
        yT=(float)(r.y+r.height/2);
        dx=(float)(0.20*scale);
        dy=(float)(0.15*scale);
        path.moveTo(x0,y0);
        path.lineTo(xT,yT);
        path.lineTo(xT+dx,yT+dy);
        path.moveTo(xT+dx,yT-dy);
        path.lineTo(xT,yT);
        break;
    }
    g.draw(path);
    
  }

}
